//
//  wis_client.m
//  wAudioShare
//
//  Created by 赵帅 on 13-12-13.
//  Copyright (c) 2013年 赵帅. All rights reserved.
//

#import "WISInterfaces.h"
#import "wis.h"
#import <netinet/in.h>
#import <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <fcntl.h>
#import "pthread.h"

@implementation WISInterfaces

+(int)send_cmd:(int)socket cmdId:(int)cmdId data:(void *)data len:(int)len
{
    int ret = -1;
    int i;
    WisSocketCmd* cmd = (WisSocketCmd*)malloc(sizeof(WisSocketCmd) + len);
    
    memset((void *)cmd, 0, sizeof(WisSocketCmd) + len);
    cmd->cmdId = cmdId;
    cmd->cmdDataLength = len;
    if ( len > 0 )
        memcpy(cmd->cmdData, data, len);
    len = sizeof(WisSocketCmd)+cmd->cmdDataLength;
    
    //int check = 0;
    //int check2 = 0;
    for(i=0; i<len; i++)
    {
        //char x = ((char*)cmd)[i];
        //check += x;
        
        //int y = x;
        //if(x < 0)
        //    y = (x&0x7F) + 128;
        //check2 += y;
        cmd->checksum += ((unsigned char*)cmd)[i];
    }
//    [tcpSocket sendCMD: Withhost:<#(NSString *)#> hostPort:<#(int)#> cmd:<#(NSString *)#>]
    len = send(socket, cmd, (sizeof(WisSocketCmd)+cmd->cmdDataLength), 0);
    if(len == sizeof(WisSocketCmd)+cmd->cmdDataLength)
        ret = 0;
    free(cmd);
    return ret;
}

+(int)wis_send_private_cmd:(int)socket cmdId:(int)cmd_id data:(void *)data len:(int)len uuid:(char *)uuid
{
	int count = sizeof(wis_user_private_cmd) + len;
    wis_user_private_cmd *cmd =(wis_user_private_cmd *) malloc(count);
    memset((void *)cmd, 0, sizeof(count));
    sprintf(cmd->uuid, "%s", uuid);
    cmd->cmd = cmd_id;
    cmd->arg_len = len;
	memcpy((void *)cmd->arg, data, len);
    
    int ret = [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_PRIVATE data:(void *)cmd len:count];
    
    free(cmd);
	return ret;
}

+(int)wis_send_login_cmd:(int)socket name:(char *)name
{
//    return [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_LOGIN data:name len:strlen(name)];
    
    if([[GlobalInfo sharedInstance].deviceToken length] == 0)
    {
        return [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_LOGIN data:name len:31];
    }
    else
    {
        char * sendData = malloc(32+72);
        memset(sendData, 0, 104);
        memcpy(sendData, name, 31);
        memcpy(sendData+32, [[GlobalInfo sharedInstance].deviceToken UTF8String], 72);
        int ret = [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_LOGIN data:sendData len:104];
        free(sendData);
        return ret;
    }

}

+(int)wis_send_get_devices_cmd:(int)socket
{
    return [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_GET_DEVICES data:nil len:0];
}

+(int)wis_send_heartbeat_cmd:(int)socket
{
    return [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_HEART_BEAT data:nil len:0];
}

+(int)wis_send_get_device_info:(int)socket uuid:(char *)uuid
{
    return [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_GET_DEVICE_INFO data:uuid len:32];
}

+(int)wis_send_bind_cmd:(int)socket uuid:(char *)uuid
{
    return [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_BIND data:uuid len:32];
}

+(int)wis_send_unbind_cmd:(int)socket uuid:(char *)uuid
{
    return [WISInterfaces send_cmd:socket cmdId:WIS_CMD_USER_UNBIND data:uuid len:32];
}

+(int)wis_send_uart_cmd:(int)socket text:(char *)text textLength:(int)length uuid:(char *)uuid
{
    return [WISInterfaces wis_send_private_cmd:socket cmdId:0x9000 data:(void *)text len:length uuid:uuid];
}

+(int)wis_send_cgi_url_cmd:(int)socket uuid:(char *)uuid msgid:(char *)msgid urlData:(char *)urlData {
    
    DLog(" cgi_test_get start ..\n");
    int len = 32 + sizeof(char) + (int)strlen(urlData) + 1;  // 32-uuid, 1-'\0'
    cgi_request_t *url_cmd = (cgi_request_t*)malloc(len);
    memset(url_cmd, 0, len);
    sprintf(url_cmd->uuid, "%s", uuid);// sender's uuid
    static int msg_id = 1;
    url_cmd->msg_id = msg_id++;  // request id
    memcpy((void *)url_cmd->url, urlData, strlen(urlData));
    
/*
    wis_user_private_cmd *cmd =(wis_user_private_cmd *) malloc(count);
    memset((void *)cmd, 0, sizeof(count));
    sprintf(cmd->uuid, "%s", uuid);
    cmd->cmd = cmd_id;
    cmd->arg_len = len;
    memcpy((void *)cmd->arg, data, len);
*/
    int result = [WISInterfaces send_cmd:socket cmdId:WIS_CMD_CGI_URL data:(void *)url_cmd len:len];
    free(url_cmd);
    DLog(" cgi_test_get end...\n");
    return result;
}

@end
